<?php
namespace Client\Controller;

use Zend\View\Model\ViewModel;
use Client\Common\VerifyHelper;

class IndexController extends InitializeController
{   
    
	
	protected function  getOrgProductsRecommanded($orgList){
		$orgIds=array();//组织显示 id
		$orgRs=array();//组织推荐商品
		$recRs=array();//推荐商品信息
		
		foreach($orgList as $k=>$v){
			$orgIds[] = $v['org_id'];
			$orgRs[$v['org_id']]['data'] = $v;
			$orgRs[$v['org_id']]['recommended']=array("无");
		}
		
		$recommendedList=$this->getProductTable()->getOrgProductsRecommended($orgIds);
		
		foreach($recommendedList as $k=>$v){
			if(!empty($v['product_name'])){
				$recRs[$v['org_id']][] =$v['product_name'];
			}
		}
		
		foreach($orgRs as $k =>$v){
			if(!empty($recRs[$k])){
				$orgRs[$k]['recommended']=$recRs[$k];
			}
		}
		return $orgRs;
	}
	
	
    public function indexAction(){
		$catalogList="";//类目
		$types=array();//首页类目
		$orgList=array();//单位列表
		$recommands=array();//商品推荐
		$bannerList=array();//首页banner
		
		$request=$this->getRequest();
		$catalogId=$request->getQuery("type");
        	   	
		//$cate_id = isset($_GET['cate_id']) ? $_GET['cate_id'] : 0;
		
		$catalogList = $this->getCatalogTable()->getAll();
		$bannerList = $this->getBannerTable()->getItems(1);
		
		$types= $this->getTypeTable()->getAll();
		$where=array();
		if($catalogId){
			$where['catalog_id'] = $catalogId;
		}
		$orgList=$this->getOrganizationTable()->getItems($where);
		$orgRecommends =$this->getOrgProductsRecommanded($orgList);
		
		return new ViewModel(array(
				'catalogList' => $catalogList,
				'bannerList' => $bannerList,
				'types' => $types,
				'orgRecommends' => $orgRecommends
		));
		
		//$recommends = $this->getProductTable()->getAllRecommendedProducts();
		//getAllRecommendedProducts;
		// $organization_list = $this->getCatalogTable()->findByCateId($cate_id);//单位列表
		// $org_goods_list = $this->getProductTable()->findByOrgId($organization_list);//商品列表
		// $banner_list = $this->getBannerTable()->find(1, 3);
		/**
		return new ViewModel(array(
			'cate_list' => $cate_list,
			'banner_list' => $banner_list,
			'recommend_list' => $recommend_list,
			'organization_list' => $org_goods_list,
		));
		**/

    }
    
    /**
     * 店内搜索
     */
    public function searchAction(){
    	$keyword=$_GET['keyword'];
    	$query=array();//查询条件
    	$query['keyword'] = $keyword;
    	$orgList=array();//单位列表
    	
    	$orgList=$this->getOrganizationTable()->orgSearch($query);
    	if(count($orgList)>0){
    		$orgRecommends =$this->getOrgProductsRecommanded($orgList);
    	}
    	
    	return new ViewModel(array(
    			'orgRecommends' => $orgRecommends
    	));
    
    }
    
    
/*     public function searchAction(){
        $keyword = isset($_GET['keyword']) ? VerifyHelper::text($_GET['keyword']) : '';
        if (!empty($keyword)) {
            
            $query =array(
                'keyword' => $keyword
            );
            $organization_list = $this->getOrganizationTable()->findByCateId($query);
            
            $result['paginator'] = $this->getPaginatorInfo($organization_list);
            
            $page = !empty($query['page'])?$query['page']:1;
            if ($page > $organization_list->getCurrentPageNumber()) {
                $data = array();
            } else {
                $data = $this->paginatorToArray($organization_list);
            }

            return new ViewModel();
        }
        
        return new ViewModel();
    } */

  
}